package com.lysj.bhp.supernode.controller;

import com.lysj.bhp.supernode.base.SuperNodeContent;
import com.lysj.bhp.supernode.domain.SuperNode;
import com.lysj.bhp.supernode.domain.SuperNodeUser;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("admin/super_node_user")
public class SuperNodeUserController extends SuperNodeContent {

    @InitBinder
    public void InitBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd hh:mm"), true));
    }


    @GetMapping("/list")
    public Resp list(SuperNodeUser model) {
        Specification specification = ParamUtil.createSpecification(model);
        List<SuperNodeUser> superNodeUserList = superNodeUserRepository.findAll(specification, new Sort(Sort.Direction.DESC, "createDate"));
        return new Resp(superNodeUserList);
    }


    @PostMapping("/remove")
    public Resp remove(SuperNodeUser model) {

        return superNodeUserService.remove(model);
    }


    @GetMapping("/find_node_users")
    public Resp list(Long superNodeId) {

        return new Resp(superNodeUserRepository.findBySuperNodeId(superNodeId));
    }


}
